package com.kuang.jdbc;

import org.junit.Test;

import java.sql.*;

public class getconnection {
    public static void main(String[] args) {
        String url="jdbc:mysql://localhost:8806/jdbc?useUnicode=true&characterEncoding=utf-8&UseSSL=true";
    }
    @Test
    public void getconnection() throws ClassNotFoundException, SQLException {
        String url="jdbc:mysql://localhost:8806/jdbc?useUnicode=true&characterEncoding=utf-8&UseSSL=true";
        String username="root";
        String password="123456";

        //1：加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2:连接数据库
        Connection connection = DriverManager.getConnection(url, username, password);

        //3：向数据库发送SQL的对象Statement
        Statement statement = connection.createStatement();

        //4：编写SQL并执行，并返回结果集
        String sql="select * from users";
        ResultSet resultSet = statement.executeQuery(sql);

        //5：遍历resultSet结果集，显示数据
        while (resultSet.next()){
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            String password1 = resultSet.getString("password");
            String email = resultSet.getString("email");
            Date birthday = resultSet.getDate("birthday");
            System.out.println(id + "\t" + password1 + "\t" + name + "\t" + email + "\t" + birthday);
        }
//        6：关闭连接，释放资源
        resultSet.close();
        statement.close();
        connection.close();
    }

}
